package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.firecommand.dto.PlotRoomReturnDto;
import com.telit.firecommand.entity.CadPlotRoom;
import com.telit.firecommand.service.ICadPlotRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 标绘房间 前端控制器
 * </p>
 *
 * @author yummy
 * @since 2021-07-31
 */
@RestController
@RequestMapping("/cad-plot-room")
@Api(tags = "标绘房间", description = "/cad-plot-room")
public class CadPlotRoomController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private ICadPlotRoomService cadPlotRoomService;

    @PostMapping("/add")
    @ApiOperation("添加")
    public DataReturn add(@RequestBody CadPlotRoom cadPlotRoom) {
        int value = cadPlotRoomService.add(cadPlotRoom);
        return DataReturn.ok(value);
    }

    @GetMapping("/delete/{id}")
    @ApiImplicitParam(name = "id", value = "配置ID", paramType = "path", dataType = "Integer")
    public DataReturn delete(@PathVariable("id") Integer id) {
        int value = cadPlotRoomService.delete(id);
        return DataReturn.ok(value);
    }

    @GetMapping("/deleteBatch")
    @ApiImplicitParam(name = "ids", value = "配置ID数组", paramType = "query", dataType = "integer[]")
    public DataReturn deleteBatch(Integer[] ids) {
        List<Integer> values = cadPlotRoomService.deleteBatch(ids);
        return DataReturn.ok(values);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public DataReturn update(@RequestBody CadPlotRoom cadPlotRoom) {
        cadPlotRoomService.updateData(cadPlotRoom);
        return DataReturn.ok();
    }

    @PostMapping("/search")
    @ApiOperation("条件查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页面", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页记录数", dataType = "int", paramType = "query")})
    public DataReturn findListByPage(@RequestBody(required = false) CadPlotRoom cadPlotRoom, Integer page,
                                     Integer pageCount) {
        IPage<CadPlotRoom> cadPlotRoomPage = cadPlotRoomService.findListByPage(cadPlotRoom, page, pageCount);
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("cadPlotRoom", cadPlotRoomPage.getRecords());
        retMap.put("total", cadPlotRoomPage.getTotal());
        return DataReturn.ok(retMap);
    }

    @PostMapping("/searchNoPage")
    @ApiOperation("条件查询")
    public DataReturn searchNoPage(@RequestBody(required = false) CadPlotRoom cadPlotRoom) {
        List<CadPlotRoom> cadPlotRoomList = cadPlotRoomService.searchNoPage(cadPlotRoom);
        return DataReturn.ok(cadPlotRoomList);
    }

    @GetMapping("/findOne/{id}")
    @ApiOperation("根据id获取配置实体")
    @ApiImplicitParam(name = "id", value = "配置ID", paramType = "path", dataType = "Long")
    public DataReturn findById(@PathVariable Integer id) {
        CadPlotRoom cadPlotRoom = cadPlotRoomService.findById(id);
        return DataReturn.ok(cadPlotRoom);
    }

    @GetMapping("/searchCountByFireAlarmId/{fireAlarmId}/{status}")
    @ApiOperation("根据火情ID和状态获取房间人数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态，1进入房间，0退出房间", paramType = "path", dataType = "string")})
    public DataReturn searchCountByFireAlarmId(@PathVariable Long fireAlarmId, @PathVariable String status) {
        Integer count = cadPlotRoomService.searchCountByFireAlarmId(fireAlarmId, status);
        return DataReturn.ok(count);
    }

    @GetMapping("/searchUserNameByFireAlarmId/{fireAlarmId}")
    @ApiOperation("根据火情ID查询房间用户姓名")
    @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", paramType = "path", dataType = "String")
    public DataReturn searchUserNameByFireAlarmId(@PathVariable String fireAlarmId) {
        PlotRoomReturnDto plotRoomReturnDto = cadPlotRoomService.searchUserNameByFireAlarmId(fireAlarmId);
        return DataReturn.ok(plotRoomReturnDto);
    }

    @GetMapping("/searchInRoomByFireAlarmIdAndUserId/{fireAlarmId}/{userId}")
    @ApiOperation("根据火情ID和用户ID判断是否在房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fireAlarmId", value = "火情ID", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", dataType = "long")})
    public DataReturn searchInRoomByFireAlarmIdAndUserId(@PathVariable String fireAlarmId, @PathVariable Long userId) {
        Boolean inRoomBool = cadPlotRoomService.searchInRoomByFireAlarmIdAndUserId(fireAlarmId, userId);
        return DataReturn.ok(inRoomBool);
    }

}
